//
//  AddCupLayer.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-19.
//
//

#include "AddCupLayer.h"
#include "cocostudio/cocostudio.h"
#include "ui/CocosGUI.h"
#include "cocos-ext.h"
#include "DictionaryKeyNames.h"


USING_NS_CC_EXT;
using namespace cocostudio;
using namespace ui;

#define BOTTLE_GAP     100

bool AddCupLayer::init()
{
    if (!Layer::init()) {
        return false;
    }
    cups = {Value("cup_01_d@2x.png"),Value("cup_02_d@2x.png"),Value("cup_03_d@2x.png"),Value("cup_04_d@2x.png"),Value("cup_05_d@2x.png"),Value("cup_06_d@2x.png"),Value("cup_07_d@2x.png"),Value("cup_08_d@2x.png"),
        Value("cup_09_d@2x.png"),Value("cup_10_d@2x.png")};
    opacity = {Value(100),Value(150),Value(200),Value(250),Value(300),Value(350),Value(400),Value(450),Value(500),Value(550),Value(600),Value(650),Value(700),Value(750),
    Value(800),Value(850),Value(900),Value(950),Value(1000)};
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    shaderLayer = ShaderLayer::create();
    this -> addChild(shaderLayer, 5000);
    
    popLayer = PopupLayer::create();
    this -> addChild(popLayer, 5001);
    
//    this -> addStudioUI();
    auto choiceBox = Sprite::create("choicebox_d2x.png");
    auto layout = ui::Layout::create();
    layout -> setClippingEnabled(true);
    layout -> setContentSize(choiceBox -> getContentSize());
    choiceBox -> setPosition(choiceBox -> getContentSize().width * 0.5, choiceBox -> getContentSize().height * 0.5);
    layout -> addChild(choiceBox);
    
    layout -> setAnchorPoint(Point(0.5, 0.5));
    layout -> setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
    popLayer -> addChild(layout);
    
    Sprite *filter= Sprite::create("choice_filter_d2x.png");
    filter -> setAnchorPoint(Point(0.5f, 0));
    filter -> setPosition(Point(layout -> getContentSize().width * 0.5f, 0));
    layout -> addChild(filter, 20);
    
    
    auto layout2 = ui::Layout::create();
    layout2 -> setContentSize(Size(layout -> getContentSize().width - 10, layout -> getContentSize().height));
    layout2 -> setPosition(Point(5,0));
    layout2 -> setClippingEnabled(true);
    layout -> addChild(layout2);
    
    cupsPageView = XKPageView::create(layout2 -> getContentSize(), this, 100);
    cupsPageView -> setDirection(extension::ScrollView::Direction::HORIZONTAL);
    cupsPageView -> setPosition(Point(0 , 90));
    cupsPageView -> initContentOffset(Vec2(layout2 -> getContentSize().width * 0.5f - cupsPageView -> sizeForPerPage().width * 0.5f, 0));
//    pageView -> setClippingToBounds(false);
    layout2 -> addChild(cupsPageView);
    for (int i = 1;  i <= DataCenter::getInstance() -> getCupCount() ; i++) {
//        auto cell = Sprite::create(cups.at(i).asString());
        auto cell = Sprite::create(DataCenter::getInstance() -> getCupNameByType(i));
        cell -> setScale(1.5);
        cell -> setPosition(Point(cupsPageView -> sizeForPerPage().width * 0.5f, cell -> getContentSize().height * 0.5 * cell -> getScale() ));
        cupsPageView -> addPage(cell);
    }
    
    
    opasPageView = XKPageView::create(layout -> getContentSize(), this, 101);
    opasPageView -> setDirection(extension::ScrollView::Direction::HORIZONTAL);
    opasPageView -> setPosition(Point(0 , 0));
    opasPageView -> initContentOffset(Vec2(layout -> getContentSize().width * 0.5f - opasPageView -> sizeForPerPage().width * 0.5f, 0));
    layout -> addChild(opasPageView);
    for (int i = 0; i < opacity.size(); i++) {
        auto cell = Label::createWithSystemFont(opacity.at(i).asString(),"微软雅黑", 30);
        cell -> setPosition(Point(opasPageView -> sizeForPerPage().width * 0.5f, opasPageView -> sizeForPerPage().height * 0.5f));
        opasPageView -> addPage(cell);
    }
    
    auto closeBtn = ui::Button::create("btn_close@2x.png");
    closeBtn -> setAnchorPoint(Point(0.3f, 0.7f));
    closeBtn -> addTouchEventListener(CC_CALLBACK_2(AddCupLayer::closeBtnPressed, this));
    closeBtn -> setPosition(Point(layout -> getPositionX() - layout -> getContentSize().width * 0.5f, layout -> getPositionY() + layout -> getContentSize().height * 0.5f));
    popLayer -> addChild(closeBtn);
    
    
    auto addBtn = ui::Button::create("btnempty2x.png");
    addBtn -> setTitleText("新增");
    addBtn -> addTouchEventListener(CC_CALLBACK_2(AddCupLayer::addBtnPressed, this));
    addBtn -> setScale(1.5f);
    addBtn -> setTitleFontSize(30);
    addBtn -> setTitleColor(Color3B(0,0,0));
    addBtn -> setPosition(Point(layout -> getPositionX() + layout -> getContentSize().width * 0.5f - addBtn -> getContentSize().width * 0.5 * addBtn -> getScale(),
                                layout -> getPositionY() - layout -> getContentSize().height * 0.6f -
                                addBtn -> getContentSize().height * 0.5f));
    popLayer -> addChild(addBtn);
    
    
    return true;
}

Size AddCupLayer::sizeForPerPage(XKPageView *pageView)
{
    if (pageView -> getTag() == 100) {
        return Size(160 + BOTTLE_GAP, 330);
    }
    else {
        return Size(120, 68);
    }
}

void AddCupLayer::closeBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type == ui::Widget::TouchEventType::ENDED) {
        PlayNegativeSound
        ActionInterval *action = ScaleTo::create(0.3f, 0.1f);
        CallFunc *func = CallFunc::create([&](){
            popLayer -> removeFromParentAndCleanup(true);
            shaderLayer -> removeFromParentAndCleanup(true);
        });
        popLayer -> runAction(Sequence::create(action, func, NULL));
    }
}

void AddCupLayer::addBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type == ui::Widget::TouchEventType::ENDED) {
        PlayPositiveSound
        int opa = opacity.at(opasPageView -> getCurrentIndex()).asInt();
        int index = cupsPageView -> getCurrentIndex() + 1;
        
        __Dictionary *dict = __Dictionary::create();
        dict -> setObject(__Integer::create(opa), StringUtils::format("opacity"));
        dict -> setObject(__Integer::create(index), StringUtils::format("Type"));
//        dict -> setObject(__String::create(name), StringUtils::format("fileName"));
        CCNotificationCenter::getInstance() -> postNotification("AddCup", dict);
        
        this -> closeBtnPressed(NULL, ui::Widget::TouchEventType::ENDED);
    }
}